<?php
session_start();
require_once("include/utils.php");
require_once("include/scripts.js");

if(checkSession())
{
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inventory</title>
<link href="styles/template.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="980" border="0" align="center">
  <tr>
    <td class = "header">
   	  <div class = "logo"><img src="images/CP2013group4_logo.png" alt="logo"/></div>
	  <p>
	    <!--	<ul id = "navigation">
    		<li><a href="logout.php">logout</a></li>
        	<li><a href="#">product</a></li>
        	<li><a href="inventory.php">inventory</a></li>
        	<li><a href= "home.php"><img src="images/home.png" height="21" /></a></li>
    	</ul>
	-->	
      </p>
	  <p><a href= "home.php"><img src="images/home.png" width="28" height="27" /></a>
	    <a href="inventory.php">inventory</a>
	    <a href="#">product</a>
    <a href="logout.php">logout</a></p></td>
  </tr>
  <tr>
  	
    <td>
    	<!-- left profile bar-->
      	<table id = "left_bar">
		  <tr>
		    <td class="p_head" colspan="2">User Profile</td>
	      </tr>
          <tr>
          <td class= "p_body">UserID:</td>
          <td><?php echo unserialize($_SESSION['user'])->_get_usrId(); ?></td>
          </tr>
          <tr>
          <td class= "p_body">UserName:</td>
          <td><?php echo unserialize($_SESSION['user'])->_get_usrName(); ?></td>
          </tr>
    	</table>
        <!-- left profile bar-->
        
		<!--main_content-->
        <div  id = "main_content">
        
        <!--display inventory table-->
        <form id="inventory" name="inventory" method="post" action="inventory.php">
        <table width="248" border="1" align="center">
    		<tr>
  				<td>&nbsp;</td>
    			<td width="50">No.</td>
				<td width="100">Id</td>
    			<td width="100">Name</td>
    			<td width="200">Amount</td>
            </tr>

<?php
//display inventory 

		$result = mysql_query("SELECT * FROM inventory");
		if (mysql_num_rows($result) > 0)
		{
		$_count = 1;
		$num = 0;
			while($row = mysql_fetch_row($result))
			{
		
				$inventory[$num] = new inventory();
				$inventory[$num]->_set_inv($row['0'], $row['1'], $row['2']);
?>  
    		<tr>
  				<td><input type="radio" name= "opt" value = "<?php echo $inventory[$num]->_get_invId(); ?>"/></td>
    			<td><?php echo $_count++; ?></td>
   				<td><?php echo $inventory[$num]->_get_invId(); ?></td>
				<td><?php echo $inventory[$num]->_get_invName(); ?></td>
    			<td><?php echo $inventory[$num]->_get_invAmt(); ?></td>
    		</tr>
<?php
			$num++;
	} // end of while loop
} // end of if condition
?>
		</table> <!--available actions-->
	<table align = "center">
	<tr>
		<th colspan="3">
			<input type="submit" name="edit" value="Edit item" 
				class = "enabled" onmouseover='this.className="enabled hover"' onmouseout='this.className="enabled"'/>
			<input type="submit" name="delete" value="Delete item" 
				class = "enabled" onmouseover='this.className="enabled hover"' onmouseout='this.className="enabled"'/>
			<input type="submit" name="add" value="Add" onclick='location.href="<?php echo "additem.php"?>"'
				class = "enabled" onmouseover='this.className="enabled hover"' onmouseout='this.className="enabled"'/>
		</th>
	</tr>
	</table>
    </form> <!--end of display inventory table-->
<?php 			//start edit
    if(!empty($_REQUEST['edit']))
	{
		if (!isset($_REQUEST['opt'])) 
		{
			echo "Empty selection.....";
			echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
		}
		else
		{
			$selected_item = mysql_query("SELECT * FROM inventory WHERE inv_id = '".$_REQUEST['opt']."'");
			$edit_item = mysql_num_rows($selected_item);		
			if($edit_item == 1)
			{
				$_row = mysql_fetch_row($selected_item);
				$_inv = new inventory();
				$_inv->_set_inv($_row['0'],$_row['1'],$_row['2'])
?>
	    	<form id="edit_inv" name="edit_inv" method="post" action="inventory.php">
        	<table border="0" align="center">
    			<tr>
                	<td width="10">ID</td>
    				<td width="100">Name</td>
    				<td width="100">Amount</td>
            	</tr>
				<tr>
                    <td><input name="invId" readonly="yes" type="text" value="<?php echo $_inv->_get_invId(); ?>" /></td>
					<td><input name="invName" type="text" value="<?php echo $_inv->_get_invName(); ?>" /></td>
    				<td><input name="invAmt" type="text" value="<?php echo $_inv->_get_invAmt(); ?>" /></td>
					 <td><input name="save" type="submit" value="Save"
					class = "enabled" onmouseover='this.className="enabled hover"' onmouseout='this.className="enabled"'/></td>
    				<td></td>
                </tr>
			</table>
    		</form>
<?php
			}
			} // end of if(edit_item)
	}
		// end of edit_inventory
		
		// save_item start
	if(!empty($_REQUEST['save']))
	{
			if ((!isset($_REQUEST['invName'])) or (!is_numeric($_REQUEST['invAmt'])))
			{
				echo "Invalid fields.....";
				echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
			}
			else
			{
				$_id = $_REQUEST['invId'];
				$_name = $_REQUEST['invName'];
				$_amt = $_REQUEST['invAmt'];
				$_update_item = mysql_query("UPDATE inventory SET inv_name = '".$_name."', inv_amt = '".$_amt."' WHERE inv_id = '".$_id."'") or die(mysql_error());
				echo "<h3>Selected item has been successfully updated.</h3>";
				echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
			}
	}
			
		// end of save_item
		
		// < add item start
			
    if(!empty($_REQUEST['add']))
	{

?>
	    	<form id="add_inv" name="add_inv" method="post" action="inventory.php">
        	<table border="0" align="center">
    			<tr>
    				<td width="100">Name</td>
    				<td width="100">Amount</td>
            	</tr>
				<tr>
					<td><input name="invName1" type="text" value="" /></td>
    				<td><input name="invAmt1" type="text" value="" /></td>
					 <td><input name="confirm" type="submit" value="Confirm"
					class = "enabled" onmouseover='this.className="enabled hover"' onmouseout='this.className="enabled"'/></td>
    				<td></td>
                </tr>
			</table>
    		</form>
	
<?php
	}
		if(!empty($_REQUEST['confirm']))
		{
			if (($_REQUEST['invName1']=="") or (!is_numeric($_REQUEST['invAmt1'])))
			{
				echo "Invalid fields.....";
				echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
			}
			else
			{
				$_name = $_REQUEST['invName1'];
				$_amt = $_REQUEST['invAmt1'];
				$_selected_item = mysql_query("SELECT * FROM inventory WHERE inv_name = '".$_name."'");
				if (mysql_numrows($_selected_item)==1)
				{
					$_add_item = mysql_query("UPDATE inventory SET inv_amt = inv_amt + '".$_amt."' WHERE inv_name = '".$_name."'") or die(mysql_error());
					echo "<h3>Item updated.</h3>";
					echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
				}
				else
				{
					$_add_item = mysql_query("INSERT INTO inventory (inv_name, inv_amt) VALUES ('$_name', '$_amt')") or die(mysql_error());
					echo "<h3>Item added.</h3>";
					echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
				}
			}
			
		}
		// ---------------------------------------> end of add
		// <	delete item
	if(!empty($_REQUEST['delete']))
	{
		if (!isset($_REQUEST['opt'])) 
		{
			echo "Empty selection.....";
			echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
		}
		else
		{
			$_id = $_REQUEST['opt'];
			$delete_query = mysql_query("DELETE FROM inventory WHERE inv_id = '".$_id."'") or die(mysql_error());
			if ($delete_query) echo "<h3>Selected item has been deleted</h3>";
			echo "<meta http-equiv='refresh' content=1;'inventory.php' />";
		}
	}
		// --------------------------------------->end of delete

?>
    </div>
    <!--main_content-->
    </td>
  </tr>
<?php
	include("include/footer.php");
?>
</table>
</body>
</html>
<?php
}
else
{
	echo 'You are not allow to enter this page. Please <a href="index.php">LogIn</a> First.';
	echo "<meta http-equiv='refresh' content='=2;index.php' />";	
}
?>
